package com.gogowan.agroenterprise.supply;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.IntegrateQuestInfo;
import com.gogowan.agroenterprise.model.MaterialResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.SupplyDemandResultInfo;
import com.gogowan.agroenterprise.utils.Utils;

/**
 * 第二期开始 供求发布首页 2012.12.10
 * @author Maniac
 *	
 */
public class SupplyMainActivity extends BaseActivity{
	
	
	private Button publishSupply;
	private Button publishDemand;
	private Button mySupply;
	private Button myDemand;
	private Button search;
	private Button myCollection;
	private Button recommendation;
	private Button back;
	
	private SharedPreferences preferences;
	private Editor editor;
	private ProgressDialog progressDialog;
	
	private long enterpriseId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supply_main);
		
		publishSupply = (Button)findViewById(R.id.supply_main_publish_supply);
		publishDemand = (Button)findViewById(R.id.supply_main_publish_demand);
		mySupply = (Button)findViewById(R.id.supply_main_examine_supply);
		myDemand = (Button)findViewById(R.id.supply_main_examine_demand);
		search = (Button)findViewById(R.id.supply_main_search);
		myCollection = (Button)findViewById(R.id.supply_main_my_collection);
		recommendation = (Button)findViewById(R.id.supply_main_recommendation);
		back = (Button)findViewById(R.id.supply_main_btn_exit);
		
		preferences = getSharedPreferences("LoginINfo", MODE_PRIVATE);
		enterpriseId = preferences.getLong("enterpriseid", 0);
		editor = preferences.edit();
		
		
		publishSupply.setOnClickListener(supplyCallback);
		publishDemand.setOnClickListener(demandCallback);
		mySupply.setOnClickListener(mySupplyCallback);
		myDemand.setOnClickListener(myDemandCallback);
		search.setOnClickListener(searchCallback);
		myCollection.setOnClickListener(collectionCallback);
		recommendation.setOnClickListener(recommendationCallback);
		back.setOnClickListener(backCallback);
	}

	/**
	 * 发布供应按钮点击事件
	 */
	public Button.OnClickListener supplyCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			publishSupply.setEnabled(false);
			IntegrateQuestInfo integrateQuestInfo = new IntegrateQuestInfo();
			integrateQuestInfo.setEnterpriseID(enterpriseId);
			
			progressDialog = ProgressDialog.show(SupplyMainActivity.this,
					getString(R.string.alert_title),
					getString(R.string.alert_message), true, false);
			
			GetSupplyProductTypes getSupplyProductTypes = new GetSupplyProductTypes();
			getSupplyProductTypes.execute(integrateQuestInfo);
			
		}
		
	};
	
	/**
	 * 发布求购按钮点击事件
	 */
	public Button.OnClickListener demandCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(SupplyMainActivity.this,PublishDemandActivity.class);
			startActivity(intent);
			Utils.randomAnim(SupplyMainActivity.this);
		}
		
	};
	/**
	 * 我的供应按钮点击事件
	 */
	public Button.OnClickListener mySupplyCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			IntegrateQuestInfo integrateQuestInfo = new IntegrateQuestInfo();
			integrateQuestInfo.setEnterpriseID(enterpriseId);
			integrateQuestInfo.setSdID(0);
			progressDialog = ProgressDialog.show(SupplyMainActivity.this,
					getString(R.string.alert_title),
					getString(R.string.alert_message), true, false);
			
			MYReleasesSupplyAsyn releasesSupplyAsyn = new MYReleasesSupplyAsyn();
			releasesSupplyAsyn.execute(integrateQuestInfo);
			
		}
		
	};
	
	/**
	 * 我的求购按钮点击事件
	 */
	public Button.OnClickListener myDemandCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			IntegrateQuestInfo integrateQuestInfo = new IntegrateQuestInfo();
			integrateQuestInfo.setEnterpriseID(enterpriseId);
			integrateQuestInfo.setSdID(0);
			progressDialog = ProgressDialog.show(SupplyMainActivity.this,
					getString(R.string.alert_title),
					getString(R.string.alert_message), true, false);
			
			MYReleasesDemandAsyn releasesDemandAsyn = new MYReleasesDemandAsyn();
			releasesDemandAsyn.execute(integrateQuestInfo);
			
		}
		
	};
	
	/**
	 * 搜索按钮点击事件
	 */
	public Button.OnClickListener searchCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(SupplyMainActivity.this,SearchActivity.class);
			startActivity(intent);
			Utils.randomAnim(SupplyMainActivity.this);
		}
		
	};
	
	/**
	 * 我的收藏按钮点击事件
	 */
	public Button.OnClickListener collectionCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(SupplyMainActivity.this,CollectionsActivity.class);
			startActivity(intent);
			Utils.randomAnim(SupplyMainActivity.this);
		}
		
	};
	
	/**
	 * 推荐信息按钮事件
	 */
	public Button.OnClickListener recommendationCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(SupplyMainActivity.this,RecommendationActivity.class);
			startActivity(intent);
			Utils.randomAnim(SupplyMainActivity.this);
		}
		
	};
	
	/**
	 * 退出事件
	 */
	public Button.OnClickListener backCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			SupplyMainActivity.this.finish();
			overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
			/*AlertDialog alert = new AlertDialog.Builder(SupplyMainActivity.this)
			.setTitle(getString(R.string.tip_back))
			.setMessage(getString(R.string.tip_back_description)).setCancelable(false)
					.setNegativeButton("否", new AlertDialog.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// do nothing
						}
					})
					.setPositiveButton("是", new AlertDialog.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// 结束应用程序
							SupplyMainActivity.this.finish();
							Utils.randomAnim(SupplyMainActivity.this);
						}
					}).create();
			alert.show();*/
		}
		
	};

	/*@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			AlertDialog alert = new AlertDialog.Builder(SupplyMainActivity.this)
			.setTitle(getString(R.string.tip_back))
			.setMessage(getString(R.string.tip_back_description)).setCancelable(false)
					.setNegativeButton("否", new AlertDialog.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// do nothing
						}
					})
					.setPositiveButton("是", new AlertDialog.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// 结束应用程序
							SupplyMainActivity.this.finish();
							Utils.randomAnim(SupplyMainActivity.this);
						}
					}).create();
			alert.show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
*/
	
	/**
	 * 点击供求按钮 请求产品类型数据
	 * @author Mainac
	 */
	private class GetSupplyProductTypes extends MyAsyncTask{

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(SupplyMainActivity.this, getString(R.string.server_error), Toast.LENGTH_LONG).show();
			} else {
				
				Message message = (Message) Utils.fromJSON((String) res, Message.class);
				String string = Utils.getMessageContent(message);
				if (string == null || string.equals("")) {
					Toast.makeText(SupplyMainActivity.this, getString(R.string.analyse_error), Toast.LENGTH_LONG).show();
				} else {
					MaterialResultInfo materialResultInfo = (MaterialResultInfo)Utils.fromJSON(string, MaterialResultInfo.class);
					int result = materialResultInfo.getResult();
					if (result == 6010) {
						editor.putString("MaterialResultInfo", string);
						editor.commit();
						
						Intent intent = new Intent(SupplyMainActivity.this,PublishSupplyActivity.class);
						startActivity(intent);
						Utils.randomAnim(SupplyMainActivity.this);
					} else {
						Toast.makeText(SupplyMainActivity.this, materialResultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
					}
				}
			}
			publishSupply.setEnabled(true);
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			IntegrateQuestInfo request = (IntegrateQuestInfo)params[0];
			String json = Utils.toJSON(request);
			Message message = new Message(6000, true, json);
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.first_url_page), message);
			return res;
		}
		
	}
	
	/**
	 * 查看我的供应列表
	 * @author Maniac
	 * 2012.12.12
	 */
	private class MYReleasesSupplyAsyn extends MyAsyncTask{

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(SupplyMainActivity.this, getString(R.string.server_error), Toast.LENGTH_LONG).show();
			} else {
				
				Message message = (Message) Utils.fromJSON((String) res, Message.class);
				String string = Utils.getMessageContent(message);
				if (string == null || string.equals("")) {
					Toast.makeText(SupplyMainActivity.this, getString(R.string.analyse_error), Toast.LENGTH_LONG).show();
				} else {
					
					SupplyDemandResultInfo supplyDemandResultInfo = (SupplyDemandResultInfo)Utils.fromJSON(string, SupplyDemandResultInfo.class);
					int result = supplyDemandResultInfo.getResult();
					if (result == 6510) {
						Intent intent = new Intent(SupplyMainActivity.this,MySupplyListActivity.class);
						intent.putExtra("myReleases", string);
						startActivity(intent);
						Utils.randomAnim(SupplyMainActivity.this);
					} else {
						Intent intent = new Intent(SupplyMainActivity.this,MySupplyListActivity.class);
						startActivity(intent);
						Utils.randomAnim(SupplyMainActivity.this);
						//Toast.makeText(SupplyMainActivity.this, supplyDemandResultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
					}
				}
			}
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			
			IntegrateQuestInfo request = (IntegrateQuestInfo)params[0];
			String json = Utils.toJSON(request);
			Message message = new Message(6500, true, json);
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.first_url_page), message);
			return res;
		}
	}
	
	/**
	 * 查看我的求购列表
	 * @author Maniac
	 * 2012.12.12
	 */
	private class MYReleasesDemandAsyn extends MyAsyncTask{

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(SupplyMainActivity.this, getString(R.string.server_error), Toast.LENGTH_LONG).show();
			} else {
				
				Message message = (Message) Utils.fromJSON((String) res, Message.class);
				String string = Utils.getMessageContent(message);
				if (string == null || string.equals("")) {
					Toast.makeText(SupplyMainActivity.this, getString(R.string.analyse_error), Toast.LENGTH_LONG).show();
				} else {
					
					SupplyDemandResultInfo supplyDemandResultInfo = (SupplyDemandResultInfo)Utils.fromJSON(string, SupplyDemandResultInfo.class);
					int result = supplyDemandResultInfo.getResult();
					if (result == 7510) {
						Intent intent = new Intent(SupplyMainActivity.this,MyDemandListActivity.class);
						intent.putExtra("myReleases", string);
						startActivity(intent);
						Utils.randomAnim(SupplyMainActivity.this);
					} else {
						//Toast.makeText(SupplyMainActivity.this, supplyDemandResultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
						Intent intent = new Intent(SupplyMainActivity.this,MyDemandListActivity.class);
						startActivity(intent);
					}
				}
			}
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			
			IntegrateQuestInfo request = (IntegrateQuestInfo)params[0];
			String json = Utils.toJSON(request);
			Message message = new Message(7500, true, json);
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.first_url_page), message);
			return res;
		}
		
	}
}
